package com.example.fish.controller;

import com.example.fish.pojo.LikeDetail;
import com.example.fish.pojo.User;
import com.example.fish.service.LikeInfoService;
import com.example.fish.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/likeInfo")
public class LikeInfoController extends BaseController{
    @Autowired
    private LikeInfoService likeInfoService;

//    是否喜欢过
    @PostMapping("/isLiked")
    public JsonResult<Boolean> isLiked(Integer blogid, HttpSession session){
        User user =(User) session.getAttribute("user");
        return new JsonResult<>(200,"success",likeInfoService.isLiked(user.getUserid(), blogid));
    }
//    保存点赞信息
    @PostMapping("/save")
    public JsonResult<Void> saveLike(Integer blogid, HttpSession session){
        User user =(User) session.getAttribute("user");
        likeInfoService.save(blogid,user.getUserid());
        return new JsonResult<>(200,"success",null);
    }
//    删除
    @PostMapping("/delete")
    public JsonResult<Void> deleteLike(Integer blogid, HttpSession session){
        User user =(User) session.getAttribute("user");
        likeInfoService.delete(blogid,user.getUserid());
        return new JsonResult<>(200,"success",null);
    }

//    查询所有喜欢
    @GetMapping("/getAll")
    public JsonResult<List<LikeDetail>> getAll(HttpSession session){
        User user =(User) session.getAttribute("user");
        List<LikeDetail> list = likeInfoService.findByUserid(user.getUserid());
        return new JsonResult<>(200,"success",list);
    }
}
